package com.niuzi.servlet;



import com.niuzi.entity.Admin;
import com.niuzi.util.DataUtil;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author Administrator
 */
@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username=req.getParameter("username");
        String password=req.getParameter("password");


        String inputCode = req.getParameter("code");


        HttpSession session = req.getSession();
        String code = (String)session.getAttribute("code");

        if(inputCode == null||!inputCode.equals(code)){
            resp.sendRedirect("login.jsp?status=2");
            return;
        }
        session.removeAttribute("code");
        session.removeAttribute("code_create_time");

        Admin admin = DataUtil.queryAdmin(username,password);

        if(admin!=null){
            req.getSession().setAttribute("login_user",admin);

            resp.sendRedirect("index.jsp");
        }else{
            resp.sendRedirect("login.jsp?status=1");
        }
    }
}
